package tobsn.locationcalendar.map.overlays;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;

public class EditPONOverlay extends ItemizedOverlay<NotificationOverlayItem> {

	private List<NotificationOverlayItem> mOverlayItems = new ArrayList<NotificationOverlayItem>();
	private Context mContext;
	private GeoPoint mPoint;

	public EditPONOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		populate();
	}

	public EditPONOverlay(Drawable defaultMarker, Context context, GeoPoint p) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		mPoint = p;
		NotificationOverlayItem item = new NotificationOverlayItem(p, null,
				null);
		mOverlayItems.add(item);
		populate();
	}

	/**
	 * When the user presses on any location on the map. Is fired before
	 * onTap(index), which
	 */
	@Override
	public boolean onTap(GeoPoint p, MapView mapView) {
		boolean tapped = super.onTap(p, mapView);
		if (tapped) {
			// Already existing item. onTap(int index) handles this case
			// automatically
			return false;
		} else {
			// do what you want to do when you DONT hit an item
			mPoint = p;
			updateOverlay(p);

			return true;
		}
	}

	private void updateOverlay(GeoPoint p) {
		NotificationOverlayItem item = new NotificationOverlayItem(p, null,
				null);
		mOverlayItems.clear();
		mOverlayItems.add(item);
		populate();
	}

	@Override
	protected NotificationOverlayItem createItem(int i) {
		return mOverlayItems.get(i);
	}

	@Override
	public int size() {
		return mOverlayItems.size();
	}

	public GeoPoint getPoint() {
		return mPoint;
	}
}
